<%-- 
    Document   : makeLab
    Created on : Jun 29, 2011, 1:35:14 PM
    Author     : Tyler Powell
--%>

<%@page import="java.util.NoSuchElementException"%>
<%@page import="java.util.Scanner"%>
<%@page import="java.util.ArrayList"%>
<%@include file="include_header.jsp"%>
<%@include file="verifySession.jsp" %>
<jsp:useBean id="courseData" scope="application" class="edu.gatech.gellinservlet.model.CourseManager" />
<jsp:useBean id="labData" scope="application" class="edu.gatech.gellinservlet.model.LabManager" />
<jsp:useBean id="teamData" scope="application" class="edu.gatech.gellinservlet.model.TeamManager" />

<%
if (aUser instanceof Student)
{
    response.sendRedirect("stude_home.jsp");
}
else
{
%>

<% //make a new lab with given values
    String name = request.getParameter("name");
    String course = request.getParameter("course");
    String teamType = request.getParameter("teamType");
    String size = request.getParameter("size");
    
    String startDate = request.getParameter("startDate");
    String startTime = request.getParameter("startTime");
    String endDate = request.getParameter("endDate");
    String endTime = request.getParameter("endTime");
    
    String redirectPage = "";
    
    int teamDesignation = 0;
    
    if("none".equals(teamType)){
        teamDesignation = Lab.NOTEAM;
   
    }
    else if("random".equals(teamType)){
        teamDesignation = Lab.RANDOMTEAM;
   
    }
    else if("self".equals(teamType)){
        teamDesignation = Lab.PICKTEAM;
   
    }
    try
    {
        Course aCourse;
        if(aUser instanceof SystemAdmin)
        {
           aCourse = courseData.search(course);
        }
        else
        {
           aCourse = courseData.searchExtern(course, ((Instructor)aUser).getCourseList());
        }
        
        ArrayList<Student> courseStudents = aCourse.getStudentList();
        if(size.equals(""))
        {
            size = "5"; //default team size
        }
        Scanner scan = new Scanner(size);
        int teamSize;
        if(teamDesignation == Lab.NOTEAM)
        {
            teamSize = 0;
        }
        else
        {
            teamSize = scan.nextInt();
        }
            
        Lab aLab = courseData.addLabToCourse(name, teamSize, teamDesignation, aCourse.getID(), labData);
        aLab.setStartDate(startDate);
        aLab.setStartTime(startTime);
        aLab.setEndDate(endDate);
        aLab.setEndTime(endTime);
        
        labData.add(aLab);
        int teamCounter = 0;
        if(aLab.getTeamDesignation() == Lab.RANDOMTEAM)
        {
            while(aLab.getTeamlessStudents().size() > 0)
            {
                teamCounter++;
                Team aTeam = labData.addTeamToLab(aLab.getName() + "-Team " + teamCounter, aLab.getID(), teamData);
                teamData.add(aTeam);
            }
        }
        
    }
    catch(Exception e)
    {
        redirectPage="createLab.jsp?code=invalid";
    }
    if(redirectPage.equals(""))
    {
        response.sendRedirect("createLab.jsp?code=valid");
    }
    else
    {
        response.sendRedirect(redirectPage);
    }
    teamData.save("teamData.ser");
    courseData.save("courseData.ser");
    labData.save("labData.ser");

    

%>
<% } //ends student_redirect else statement made above%>
<% } //ends verifySession.jsp, all html is included in else statment %>
